//index.js
//获取应用实例
const app = getApp()
const Api = require("../../utils/api.js").API
const Store = require("../../utils/store.js").store
const codeZ = require("../../utils/util.js")
Page({
  data: { 
    item: {},
    goodsName: '',
    goodsSynopsis: '',
    catenation: '',
    files: [],
    spus: [],
    getCatenationTypes: [
        {label: "淘宝", value: "109401"},
        {label: "京东", value: "109402"}
    ],
    show: false,
    showCatenationType: false,
    spu: {},
    catenationType: {},
    spuNo: '',
    spuName: ''
  },
  onLoad: function() {
    let _this = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function(data) {
      console.log(data.item)
      _this.setData({
        item: data.item,
        files: data.item.file.split(','),
        spuNo: data.item.spuNo
      })
    })
    this.getSpu()
  },
  onShow:function(){
      // this.getSpu()
  }, 
  zoomPic: function(e) {
    let url = e.currentTarget.dataset.url
    console.log(e.currentTarget.dataset.url)
    var imgList = this.data.files;//获取data-list
      //图片预览
      wx.previewImage({
        current: url, // 当前显示图片的http链接
        urls: imgList // 需要预览的图片http链接列表
      })

  },
  copy: function() {
    let _this = this
    wx.setClipboardData({
      data: _this.data.item.catenation,
      success (res) {
        wx.getClipboardData({
          success (res) {
            console.log(res.data) // data
          }
        })
      }
    })
  },
  toSaveGoodsLook: function() {
    wx.navigateTo({
      url: '../GoodsLookAdd/index',
    })
  },
  toSaveGoodsLookWithGoods: function() {
    // console.log(this.data.item)
    let item = this.data.item
    wx.navigateTo({
      url: '../GoodsLookAdd/index',
      success: function(res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromGoodsLookDetailPage', { item: item })
      }
    })
  },
  getSpu: function() {
      let _this = this
    Api.getSpusApi().then(res => {
      let spuNo = _this.data.spuNo
      for (var i=0; i<res.data.length; i++) {
        let spu = res.data[i]
        if (spuNo == spu.spuNo) {
          _this.setData({
            spuName: spu.spuName
          })
          return
        }
      }
        // if (res.code == 0) {
        //     _this.setData({
        //         spus: res.data
        //     })
        // }
    })
  },
  uploadPic: function() {
    var _this = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'], //
            success: function(res) {
                var tempFilePaths = res.tempFilePaths;
                 let a = `list.fileInfoReqs[0].attUrl`;
                tempFilePaths.forEach((item,i)=>{
                    Api.ZhuFuUploadApi(item,{}).then(resa=>{ 
                         var resUrl = JSON.parse(resa)
                         if (resUrl.code === 0) {
                            let files = _this.data.files
                            files.push(resUrl.url)
                            _this.setData({
                                files: files
                            })
                            console.log(files)
                         }
                    });
                    
                })              
            }
        })
    },
})
